package com.keqing.tomato.util;

import static android.content.Context.MODE_PRIVATE;
import static com.keqing.tomato.util.PreferenceUtil.MySetting.default_auto_rest;
import static com.keqing.tomato.util.PreferenceUtil.MySetting.default_focus_sound;
import static com.keqing.tomato.util.PreferenceUtil.MySetting.default_focus_sound_path;
import static com.keqing.tomato.util.PreferenceUtil.MySetting.default_focus_time;
import static com.keqing.tomato.util.PreferenceUtil.MySetting.default_rest_sound;
import static com.keqing.tomato.util.PreferenceUtil.MySetting.default_rest_sound_path;
import static com.keqing.tomato.util.PreferenceUtil.MySetting.default_rest_time;
import static com.keqing.tomato.util.PreferenceUtil.MySetting.default_vibration;

import android.content.Context;
import android.content.SharedPreferences;

import com.keqing.tomato.bean.Sound;

import java.util.ArrayList;
import java.util.List;

public class PreferenceUtil {
    private static final String settingFile = "mySetting";
    private static SharedPreferences.Editor editor = null;
    private static SharedPreferences reader = null;
    private static MySetting mySetting;

    private static void getEditor(Context context) {
        if (editor == null) {
            editor = context.getSharedPreferences(settingFile, MODE_PRIVATE).edit();
        }
    }

    private static void getReader(Context context) {
        if (reader == null) {
            reader = context.getSharedPreferences(settingFile, MODE_PRIVATE);
        }
    }

    public static MySetting getMySetting(Context context) {
        if (mySetting == null) {
            getEditor(context);
            getReader(context);
            mySetting = new MySetting(
                    reader.getInt("focusTime", default_focus_time),
                    reader.getInt("restTime", default_rest_time),
                    reader.getString("focusSound", default_focus_sound),
                    reader.getString("focusSoundPath", default_focus_sound_path),
                    reader.getString("restSound", default_rest_sound),
                    reader.getString("restSoundPath", default_rest_sound_path),
                    reader.getBoolean("vibration", default_vibration),
                    reader.getBoolean("autoRest", default_auto_rest)
            );
        }
        return mySetting;
    }


    public static class MySetting {
        public static final int default_focus_time = 25;
        public static final int default_rest_time = 5;
        public static final String default_focus_sound = "叮一声";
        public static final String default_focus_sound_path = "sound/Bell1.mp3";
        public static final String default_rest_sound = "叮两声";
        public static final String default_rest_sound_path = "sound/Bell2.mp3";
        public static final boolean default_vibration = true;
        public static final boolean default_auto_rest = false;
        public static final List<Sound> soundList = new ArrayList<Sound>() {{
            add(new Sound("", "无"));
            add(new Sound("sound/Beeps.mp3", "哔~"));
            add(new Sound("sound/Bell1.mp3", "叮一声"));
            add(new Sound("sound/Bell2.mp3", "叮两声"));
            add(new Sound("sound/Bicycle Bell.mp3", "单车铃声"));
            add(new Sound("sound/Birdcall.mp3", "鸟叫声"));
            add(new Sound("sound/Done.wav", "完成音"));
            add(new Sound("sound/Drum Cymbal Crash.mp3", "鼓点"));
            add(new Sound("sound/Failed.wav", "失败音"));
            add(new Sound("sound/Fanfare.mp3", "狂热音"));
            add(new Sound("sound/Musical1.mp3", "乐章一"));
            add(new Sound("sound/Musical2.mp3", "乐章二"));
            add(new Sound("sound/Musical3.mp3", "乐章三"));

        }};
        public static final int[] focus_time_array = {10, 25, 30, 35, 40, 45, 50, 60, 70, 80, 90};
        public static final int[] rest_time_array = {5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 60, 70, 80, 90};


        private int focusTime;
        private int restTime;
        private String focusSound;
        private String focusSoundPath;
        private String restSound;
        private String restSoundPath;
        private boolean vibration;
        private boolean autoRest;

        public MySetting() {
        }

        public MySetting(int focusTime, int restTime, String focusSound, String focusSoundPath, String restSound,
                         String restSoundPath, boolean vibration, boolean autoRest) {
            this.focusTime = focusTime;
            this.restTime = restTime;
            this.focusSound = focusSound;
            this.focusSoundPath = focusSoundPath;
            this.restSound = restSound;
            this.restSoundPath = restSoundPath;
            this.vibration = vibration;
            this.autoRest = autoRest;
        }

        public int getFocusTime() {
            return focusTime;
        }

        public void setFocusTime(int focusTime) {
            this.focusTime = focusTime;
            editor.putInt("focusTime",focusTime);
            editor.apply();
        }

        public int getRestTime() {
            return restTime;
        }

        public void setRestTime(int restTime) {
            this.restTime = restTime;
            editor.putInt("restTime",restTime);
            editor.apply();
        }

        public String getFocusSound() {
            return focusSound;
        }

        public void setFocusSound(String focusSound) {
            this.focusSound = focusSound;
            editor.putString("focusSound",focusSound);
            editor.apply();
        }

        public String getFocusSoundPath() {
            return focusSoundPath;
        }

        public void setFocusSoundPath(String focusSoundPath) {
            this.focusSoundPath = focusSoundPath;
            editor.putString("focusSoundPath", focusSoundPath);
            editor.apply();
        }

        public String getRestSound() {
            return restSound;
        }

        public void setRestSound(String restSound) {
            this.restSound = restSound;
            editor.putString("restSound", restSound);
            editor.apply();
        }

        public String getRestSoundPath() {
            return restSoundPath;
        }

        public void setRestSoundPath(String restSoundPath) {
            this.restSoundPath = restSoundPath;
            editor.putString("restSoundPath", restSoundPath);
            editor.apply();
        }

        public boolean isVibration() {
            return vibration;
        }

        public void setVibration(boolean vibration) {
            this.vibration = vibration;
            editor.putBoolean("vibration", vibration);
            editor.apply();
        }

        public boolean isAutoRest() {
            return autoRest;
        }

        public void setAutoRest(boolean autoRest) {
            this.autoRest = autoRest;
            editor.putBoolean("autoRest", autoRest);
            editor.apply();
        }
    }
}
